﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Obligatorio.DataAccess;

public partial class Users_Users : System.Web.UI.Page, IUsersView
{
    #region Fields
    UsersPresenter _presenter;
    #endregion

    #region Properties

    public IEnumerable<Role> Roles
    {
        set { this.lbxRoles.DataSource = value; }
    }

    public IEnumerable<Role> UserRoles
    {
        set { this.lbxUserRoles.DataSource = value; }
    }


    public IEnumerable<int> SelectedRoles
    {
        get
        {
            List<int> ret = new List<int>();
            foreach (ListItem item in this.lbxRoles.Items)
            {
                if (item.Selected)
                {
                    ret.Add(Convert.ToInt32(item.Value));
                }
            }
            return ret;
        }
    }

    public IEnumerable<int> SelectedUserRoles
    {
        get
        {
            List<int> ret = new List<int>();
            foreach (ListItem item in this.lbxUserRoles.Items)
            {
                if (item.Selected)
                {
                    ret.Add(Convert.ToInt32(item.Value));
                }
            }
            return ret;
        }
    }


    public long UserId
    {
        get { return Convert.ToInt64(this.GridView1.SelectedValue); }
    }


    public IEnumerable<Branch> Branches
    {
        private get;
        set;
    }

    #endregion
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        GridView1.EnableDynamicData(typeof(User));
        _presenter = new UsersPresenter(this);
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        _presenter.GetBranches();
        if (!IsPostBack)
        {
            var ddl = (DropDownList)DetailsView1.FindControl("ddlBranches");
            ddl.DataSource = this.Branches;
            ddl.DataBind();
        }
    }
    protected void ImageButton_Click1(object sender, ImageClickEventArgs e)
    {
        DetailsView1.Visible = true;
    }
    protected void DetailsView1_ItemInserted(object sender, DetailsViewInsertedEventArgs e)
    {
        DetailsView1.Visible = false;
        GridView1.DataBind();
    }
    protected void DetailsView1_ItemInserting(object sender, DetailsViewInsertEventArgs e)
    {
        string username = e.Values["UserName"].ToString();

        var ddl = (DropDownList)DetailsView1.FindControl("ddlBranches");

        e.Values.Add("UserPassword", Utils.GetMD5(username));
        e.Values.Add("BranchId", Convert.ToInt16(ddl.SelectedValue));

    }
    protected void imgRoles_Click(object sender, ImageClickEventArgs e)
    {
        DetailsView1.Visible = false;
    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        DetailsView1.Visible = false;
        divRoles.Visible = true;
        _presenter.LoadRoles();

    }

    public void Refresh()
    {
        this.lbxUserRoles.DataBind();
        this.lbxRoles.DataBind();
    }


    protected void imgLeft_Click(object sender, ImageClickEventArgs e)
    {
        _presenter.RemoveRoles();
    }
    protected void imgRight_Click(object sender, ImageClickEventArgs e)
    {
        _presenter.AddRoles();
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType== DataControlRowType.DataRow)
        {
            User user = (e.Row.DataItem as User);

            this._presenter.GetBranches();

            if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
            {
                Label txtBranchName = (Label)e.Row.FindControl("txtBranchName");
                txtBranchName.Text = this.Branches.Where(b => b.BranchId == user.BranchId).Single().BranchName;
            }
            if (e.Row.RowState == DataControlRowState.Edit)
            {
                DropDownList ddl = (DropDownList)e.Row.FindControl("ddlBranches");

                ddl.DataSource = this.Branches;
                ddl.DataBind();

                ddl.SelectedValue = user.BranchId.ToString();
            }
        }

    }


    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        DropDownList ddl = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("ddlBranches");

        var branchId = Convert.ToInt16(ddl.SelectedValue);
        e.NewValues.Add("BranchId", branchId);
    }
}